package com.xiangyang.pos.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.xiangyang.pos.R;
import com.xiangyang.pos.bean.OrderErrorDataBean;
import com.xiangyang.pos.logger.Logger;
import com.xiangyang.pos.utils.SharedUtils;

import java.util.List;

/**
 * Created by Administrator on 2017/8/24 0024.
 *
 * 选择支付方式
 *
 * @author YeDalin
 *
 */

public class ChoosePayActivity extends Activity {

    private TextView tvAllMoney;
    private ImageView ivKaiXin;
    private ImageView ivWeiXin;
    private ImageView ivAlipay2;
    private ImageView ivGoBack;
//    private TextView tvCheckout;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_pay);
        initView();
        setMyClick();
    }

    private void initView(){
        tvAllMoney=findViewById(R.id.tv_all_money);
        ivKaiXin=findViewById(R.id.iv_kai_xin);
        ivWeiXin=findViewById(R.id.iv_wei_xin);
        ivAlipay2=findViewById(R.id.iv_alipay2);
        ivGoBack=findViewById(R.id.iv_go_back);
//        tvCheckout=findViewById(R.id.tv_checkout);


        int all_money= (int) SharedUtils.getIntPrefs(ChoosePayActivity.this,"all_money",-1);
        tvAllMoney.setText("￥"+all_money/100.0);

    }


    private void setMyClick(){
        ivKaiXin.setOnClickListener(listener);
        ivWeiXin.setOnClickListener(listener);
        ivAlipay2.setOnClickListener(listener);
        ivGoBack.setOnClickListener(listener);
    }


    private View.OnClickListener listener=new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.iv_kai_xin:
                    Toast.makeText(ChoosePayActivity.this, "暂不支持", Toast.LENGTH_SHORT).show();
                    return;
                case R.id.iv_wei_xin:
                    setPmtTag("Weixin");
                    Toast.makeText(ChoosePayActivity.this, "请确认客户微信支付是否成功", Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(ChoosePayActivity.this,ConfirmReceivablesActivity.class);
                    intent.putExtra("pay",2);
                    startActivityForResult(intent,2001);
                    return;
                case R.id.iv_alipay2:
                    setPmtTag("Alipay");
                    break;
                case R.id.iv_go_back:
                    finish();
                    return;
            }
            customScan();
        }
    };


    //    开始扫描
    public void customScan() {
        new IntentIntegrator(this)
                .setOrientationLocked(false)
                .setCaptureActivity(ScanActivity.class) // 设置自定义的activity是CustomActivity
                .initiateScan(); // 初始化扫描
    }

    @Override
// 通过 onActivityResult的方法获取 扫描回来的 值
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==2001){
            if(data!=null){
                List<OrderErrorDataBean.OrderErrorData> order=(List<OrderErrorDataBean.OrderErrorData>)data.getSerializableExtra("list");
                Logger.e("xxxx","商品实时数量0= "+order.get(0).getNum());
                setResult(2002,data);
                finish();
            }

        }else {
            Logger.e("xxyy","code"+requestCode);
            Logger.e("xxyy","code11"+resultCode);
            IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
            if (intentResult != null) {
                if (intentResult.getContents() == null) {
                } else {
                    // ScanResult 为 获取到的字符串
                    String scanResult = intentResult.getContents();
                    SharedUtils.setStringPrefs(ChoosePayActivity.this,"auth_code",scanResult);
                    Intent intent=new Intent(ChoosePayActivity.this,ConfirmReceivablesActivity.class);
                    intent.putExtra("pay",2);
                    startActivityForResult(intent,2001);
                }
            } else {
                super.onActivityResult(requestCode, resultCode, data);
            }
        }

    }

//    存储支付方式
    private void setPmtTag(String pmt_tag){
        SharedUtils.setStringPrefs(ChoosePayActivity.this,"pmt_tag",pmt_tag);
    }


}
